package com.qianfeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.*;
import com.qianfeng.entity.Class;
import com.qianfeng.mapper.StudentMapper;
import com.qianfeng.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Transactional
    @Override
    public void addStudent(Student student) {
        student.setClazz(new Class(student.getClassId()));
        student.setEducation(new Education(student.getEducationId()));
        student.setEmployee(new Employee(student.getEmpId()));
        studentMapper.addStudent(student);

    }

    @Transactional
    @Override
    public void deleteStudent(Integer studentId) {
        studentMapper.deleteStudent(studentId);
    }

    @Transactional
    @Override
    public void updateStudent(Student student) {
        student.setClazz(new Class(student.getClassId()));
        student.setEducation(new Education(student.getEducationId()));
        student.setEmployee(new Employee(student.getEmpId()));
        studentMapper.updateStudent(student);
    }

    @Transactional(readOnly = true)
    @Override
    public Page getStudentPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo, pageSize);
        List<Student> studentList = studentMapper.getStudentList();
        PageInfo<Student> info = new PageInfo<>(studentList);
        page.setPage(info.getList());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setPageCount(info.getPages());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        return page;
    }

    @Transactional(readOnly = true)
    @Override
    public Student getStudentByStudentId(Integer studentId) {
        return studentMapper.getStudentByStudentId(studentId);
    }

    /**
     * 上一星期每日增长
     *
     * @param inTime
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<DayStudentChange> getChangeDayStudent(Date inTime) throws ParseException {
        Calendar instance = Calendar.getInstance();
        Calendar instance2 = Calendar.getInstance();
        Calendar instance3 = Calendar.getInstance();
        Calendar instance4 = Calendar.getInstance();
        Calendar instance5 = Calendar.getInstance();
        Calendar instance6 = Calendar.getInstance();
        Calendar instance7 = Calendar.getInstance();

        ArrayList<DayStudentChange> list = new ArrayList<>();
        //上一周
        //今天是本周第几天
        int i = instance.get(Calendar.DAY_OF_WEEK)-1;
        System.out.println("今天是本周第几天"+i);
        //上周一
        instance.add(Calendar.DATE,-i-6);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String Monday = simpleDateFormat.format(instance.getTime());
        Date parse = simpleDateFormat.parse(Monday);
        Integer changeDayStudent = studentMapper.getChangeDayStudent(parse);
        DayStudentChange dayStudentChange = new DayStudentChange("周一", changeDayStudent, "周一新增");
        list.add(dayStudentChange);
        System.out.println("上周一"+Monday);
        //上周二
        instance2.add(Calendar.DATE,-i-5);
        String Tuesday = simpleDateFormat.format(instance2.getTime());
        Date parse2 = simpleDateFormat.parse(Tuesday);
        Integer changeDayStudent2 = studentMapper.getChangeDayStudent(parse2);
        DayStudentChange dayStudentChange2 = new DayStudentChange("周二", changeDayStudent2, "周二新增");
        list.add(dayStudentChange2);
        System.out.println("上周二"+Tuesday);
        //上周三
        instance3.add(Calendar.DATE,-i-4);
        String Wednesday= simpleDateFormat.format(instance3.getTime());
        Date parse3 = simpleDateFormat.parse(Wednesday);
        Integer changeDayStudent3 = studentMapper.getChangeDayStudent(parse3);
        DayStudentChange dayStudentChange3 = new DayStudentChange("周三", changeDayStudent3, "周三新增");
        list.add(dayStudentChange3);
        System.out.println("上周三"+Wednesday);
        //上周四
        instance4.add(Calendar.DATE,-i-3);
        String Thursday= simpleDateFormat.format(instance4.getTime());
        Date parse4 = simpleDateFormat.parse(Thursday);
        Integer changeDayStudent4 = studentMapper.getChangeDayStudent(parse4);
        DayStudentChange dayStudentChange4 = new DayStudentChange("周四", changeDayStudent4, "周四新增");
        list.add(dayStudentChange4);
        System.out.println("上周四"+Thursday);
        //上周五
        instance5.add(Calendar.DATE,-i-2);
        String Friday= simpleDateFormat.format(instance5.getTime());
        Date parse5 = simpleDateFormat.parse(Friday);
        Integer changeDayStudent5 = studentMapper.getChangeDayStudent(parse5);
        DayStudentChange dayStudentChange5 = new DayStudentChange("周五", changeDayStudent5, "周五新增");
        list.add(dayStudentChange5);
        System.out.println("上周五"+Friday);
        //上周六
        instance6.add(Calendar.DATE,-i-1);
        String Saturday= simpleDateFormat.format(instance6.getTime());
        Date parse6 = simpleDateFormat.parse(Saturday);
        Integer changeDayStudent6 = studentMapper.getChangeDayStudent(parse6);
        DayStudentChange dayStudentChange6 = new DayStudentChange("周六", changeDayStudent6, "周六新增");
        list.add(dayStudentChange6);
        System.out.println("上周六"+Saturday);
        //上周日
        instance7.add(Calendar.DATE,-i);
        String Sunday= simpleDateFormat.format(instance7.getTime());
        Date parse7 = simpleDateFormat.parse(Sunday);
        Integer changeDayStudent7 = studentMapper.getChangeDayStudent(parse7);
        DayStudentChange dayStudentChange7 = new DayStudentChange("周日", changeDayStudent7, "周日新增");
        list.add(dayStudentChange7);
        System.out.println("上周日"+Sunday);
        return list;
    }
    /**
     * 前六个月的增长趋势
     *
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public HomeMonthChange getHomeMonthChange() throws ParseException {
        ArrayList<Integer> changes = new ArrayList<>();
        ArrayList<String> months = new ArrayList<>();
        for (int i = 6; i > 0; i--) {
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.MONTH,-i);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
            String format = simpleDateFormat.format(instance.getTime());
//            System.out.println(format);
            Date parse = simpleDateFormat.parse(format);
//            System.out.println(parse);
            Integer changeDayStudent = studentMapper.getChangeHomeMonthChange(i);
//            System.out.println(changeDayStudent);
            months.add(format+"月");
            changes.add(changeDayStudent);
        }
        HomeMonthChange homeMonthChange = new HomeMonthChange(changes, months);
        return homeMonthChange;
    }



}
